//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package app;

import freemarker.template.Template;
import freemarker.template.TemplateException;
import io.github.yedaxia.apidocs.DocContext;
import io.github.yedaxia.apidocs.IPluginSupport;
import io.github.yedaxia.apidocs.Resources;
import io.github.yedaxia.apidocs.Utils;
import io.github.yedaxia.apidocs.parser.ControllerNode;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MarkdownDocPlugin implements IPluginSupport {
    /**
     * 服务器ip地址:端口号
     */
    private String ipAddr = "http://localhost:8888";
    /**
     * 环境(例：开发、测试、生产)
     */
    private String environment = "测试";

    public MarkdownDocPlugin() {
    }

    /**
     *
     * @param environment   环境
     * @param ipAddr        服务器ip地址:端口号
     */
    public MarkdownDocPlugin(String environment, String ipAddr) {
        this.ipAddr = ipAddr;
        this.environment = environment;
    }
    public void execute(List<ControllerNode> controllerNodeList) {
        FileWriter docFileWriter = null;

        try {
            Template ctrlTemplate = this.getDocTpl();
            String docFileName = String.format("%s-%s-api-docs.md", DocContext.getDocsConfig().getProjectName(), DocContext.getDocsConfig().getApiVersion());
            File docFile = new File(DocContext.getDocPath(), docFileName);
            docFileWriter = new FileWriter(docFile);
            Map<String, Object> data = new HashMap();
            data.put("controllerNodes", controllerNodeList);
            data.put("currentApiVersion", DocContext.getCurrentApiVersion());
            data.put("projectName", DocContext.getDocsConfig().getProjectName());
            data.put("i18n", DocContext.getI18n());
            data.put("ipAddr", ipAddr);
            data.put("environment", environment);
            ctrlTemplate.process(data, docFileWriter);
        } catch (IOException | TemplateException var10) {
            var10.printStackTrace();
        } finally {
            Utils.closeSilently(docFileWriter);
        }

    }

    private Template getDocTpl() throws IOException {
        return Resources.getFreemarkerTemplate("api-doc.md.ftl");
    }
}
